package com.whp.yky.yky.service;


import com.whp.yky.yky.entity.poentity.User;
import com.whp.yky.yky.entity.voentity.VoUser;
import com.whp.yky.yky.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.PrivateKey;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;



    public List<User> getUserInfoByUserName(String username){
        return userMapper.getUserInfoByUserName(username);
    }

    public User getUserInfo(){
        return userMapper.getUserInfo();
    }

    public boolean insertUserInfo(User user){

        if (user.getId()!=null&&!"".equals(user.getId())){

            try {
                int effecteNum= userMapper.insertUserInfo(user);
                if(effecteNum>0){
                    //如果影响行数大于0，那么就是增加成功
                    System.out.println("增加成功，主键为："+user.getId());
                    return true;
                }else {
                    throw  new RuntimeException("插入信息失败,插入行数有误");
                }
            }catch (Exception e){
                throw  new RuntimeException("插入信息失败了:"+e.getMessage());
            }
        }else{
            throw new RuntimeException("信息不能为空！！！！");
        }

    }

    public boolean updateUserInfo(User user){

        if (user.getId()!=null&&!"".equals(user.getId())){

            try {
                int effecteNum= userMapper.updateUserInfo(user);
                if(effecteNum>0){
                    //如果影响行数大于0，那么就是增加成功
                    System.out.println("修改成功，主键为："+user.getId());
                    return true;
                }else {
                    throw  new RuntimeException("修改失败,插入行数有误");
                }
            }catch (Exception e){
                throw  new RuntimeException("修改失败:"+e.getMessage());
            }
        }else{
            throw new RuntimeException("信息不能为空！！！！");
        }

    }


    public boolean deleteUserById(String id) {

        try {
            //对mysql数据库做删除操作后，影响的行数effecteNum
            int effecteNum= userMapper.deleteUserById(id);
            if (effecteNum>0){
                return true;
            }else {
                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public VoUser login(VoUser user){


        User user1 = new User();
        user1.setUsername(user.getUsername());
        user1.setPassword(user.getPassword());
        if(userMapper.loginInfo(user1)!=null){
            return user;
        }else{
            throw new RuntimeException("用户名或密码错误");
        }
    }




}
